package com.helper.dream.a361phonehelper.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.helper.dream.a361phonehelper.R;
import com.helper.dream.a361phonehelper.adapter.TuijianZhiboAdapter;
import com.helper.dream.a361phonehelper.bean.TuijianZhiboEntity;
import com.helper.dream.a361phonehelper.inter.ITuijianZhiboInterface;
import com.helper.dream.a361phonehelper.utils.ApiManager;
import com.helper.dream.a361phonehelper.utils.L;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by LamYu on 16/12/19/0019.
 */

public class TuijianZhiboFragment extends Fragment {
    View view;
    LayoutInflater inflater;
    List<TuijianZhiboEntity.DataBeanX> list=new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater=inflater;
        view = inflater.inflate(R.layout.fragment_tuijianzhibo, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LinearLayout ll= (LinearLayout) view.findViewById(R.id.ll_tj_zb);
        getData(ll);
    }

    private void getData(final LinearLayout v) {
        Retrofit retrofit=new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create())
                .baseUrl(ApiManager.TjZbBase).build();
        ITuijianZhiboInterface iTuijianZhiboInterface = retrofit.create(ITuijianZhiboInterface.class);
        Call<TuijianZhiboEntity> call = iTuijianZhiboInterface.getTjZb();
        call.enqueue(new Callback<TuijianZhiboEntity>() {
            @Override
            public void onResponse(Call<TuijianZhiboEntity> call, Response<TuijianZhiboEntity> response) {
                for (int i = 0; i < response.body().getData().size(); i++) {
                    TuijianZhiboAdapter adapter=new TuijianZhiboAdapter(response.body().getData().get(i).getData(),getActivity());
                    View view=inflater.inflate(R.layout.item_tj_zb,null);
                    TextView tvName= (TextView) view.findViewById(R.id.tv_tj_zb_name);
                    L.d("tvname:"+tvName);
                    tvName.setText(response.body().getData().get(i).getName());
                    RecyclerView rv= (RecyclerView) view.findViewById(R.id.rv_tj_zb);
                    rv.setLayoutManager(new GridLayoutManager(getActivity(),2));
                    rv.setAdapter(adapter);
                    LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
                    view.setLayoutParams(params);
                    v.addView(view);
                }
            }

            @Override
            public void onFailure(Call<TuijianZhiboEntity> call, Throwable t) {
                    L.d("failed");
            }
        });
    }

}
